package com.gale.park.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gale.park.common.api.CommonPage;
import com.gale.park.common.api.CommonResult;
import com.gale.park.modules.pms.dto.CardConditionDTO;
import com.gale.park.modules.pms.model.PmsCard;
import com.gale.park.modules.pms.service.PmsCardService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gale
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/card")
public class PmsCardController {

    @Autowired
    private PmsCardService cardService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation("添加IC卡")
    public CommonResult create(@RequestBody PmsCard card) {
        boolean result = cardService.create(card);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("获取IC卡列表")
    public CommonResult<CommonPage<PmsCard>> list(@RequestBody  CardConditionDTO condition) {
        Page<PmsCard> page = cardService.list(condition);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ApiOperation("获取IC卡列表,status为0查询全部，否则只查询未入场的")
    public CommonResult listAll() {
        List<PmsCard> list = cardService.list();
        return CommonResult.success(list);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ApiOperation("通过id删除IC卡")
    public CommonResult delete(@PathVariable String id) {
        boolean result = cardService.deleteCardById(id);
        return result ? CommonResult.success(result) : CommonResult.failed();

    }

    @RequestMapping(value = "/updateInfo/{cardId}", method = RequestMethod.GET)
    @ApiOperation("通过id查询IC卡")
    public CommonResult updateInfo(@PathVariable String cardId) {
        PmsCard card = cardService.getCarById(cardId);
        return CommonResult.success(card);
    }

    @RequestMapping(value = "/update/{cardId}", method = RequestMethod.POST)
    @ApiOperation("修改IC卡")
    public CommonResult update(@RequestBody PmsCard card) {
        boolean result = cardService.update(card);

        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }


}

